/* winsio.h */

/********************/
/* SERIAL-INTERFACE */
/* for MS-Windows   */
/* (C) 2003 by NAH6 */
/********************/

#ifndef __WINSIO_H
#define __WINSIO_H
#ifdef __cplusplus
extern "C" {
#endif

/*------------*/
/*  INCLUDES  */
/*------------*/

#include <windows.h>

/*-----------*/
/*  DEFINES  */
/*-----------*/

#define E_SIO_NULLPTR         (-0x6101)
#define E_SIO_OPEN_PORTNUM    (-0x6102)
#define E_SIO_OPEN_CREATE     (-0x6103)
#define E_SIO_OPEN_GETPROP    (-0x6104)
#define E_SIO_OPEN_DEVTYPE    (-0x6105)
#define E_SIO_OPEN_NO8N1      (-0x6106)
#define E_SIO_OPEN_BAUDRATE   (-0x6107)
#define E_SIO_OPEN_FLOWCTRL   (-0x6108)
#define E_SIO_OPEN_USEDTR     (-0x6109)
#define E_SIO_OPEN_USEDSR     (-0x610A)
#define E_SIO_OPEN_USEDCD     (-0x610B)
#define E_SIO_OPEN_SETBUFFER  (-0x610C)
#define E_SIO_OPEN_SETMODE    (-0x610D)
#define E_SIO_OPEN_SETTIMER   (-0x610E)
#define E_SIO_OPEN_SETDTR     (-0x610F)
#define E_SIO_OPEN_GETLINE    (-0x6110)
#define E_SIO_OPEN_NODSR      (-0x6111)
#define E_SIO_OPEN_NOCTS      (-0x6112)
#define E_SIO_OPEN_DCDON      (-0x6113)
#define E_SIO_OPEN_CLRRXERR   (-0x6114)
#define E_SIO_OPEN_CLRRXBUF   (-0x6115)
#define E_SIO_OPEN_OBXNUM     (-0x6116)
#define E_SIO_OPEN_OBXREGOP   (-0x6117)
#define E_SIO_OPEN_OBXREGRD   (-0x6118)
#define E_SIO_OPEN_OBXREGTYP  (-0x6119)
#define E_SIO_OPEN_OBXREGLEN  (-0x611A)
#define E_SIO_OPEN_OBXREGWR   (-0x611B)
#define E_SIO_OPEN_OBXDEV     (-0x611C)
#define E_SIO_OPEN_OBXDCTL    (-0x611D)
#define E_SIO_OPEN_RILNUM     (-0x611E)
#define E_SIO_OPEN_RILDLL     (-0x611F)
#define E_SIO_OPEN_RILFKTN    (-0x6120)
#define E_SIO_OPEN_RILEVT     (-0x6121)
#define E_SIO_OPEN_RILINIT    (-0x6122)
#define E_SIO_OPEN_RILSYNC1   (-0x6123)
#define E_SIO_OPEN_RILSYNC2   (-0x6124)
#define E_SIO_OPEN_RILSYNC3   (-0x6125)
#define E_SIO_OPEN_RILDEV     (-0x6126)
#define E_SIO_OPEN_RILDCTL    (-0x6127)
#define E_SIO_OPEN_RILSCTL    (-0x6128)
#define E_SIO_CLOSE_SETDTR    (-0x6129)
#define E_SIO_CLOSE_HANDLE    (-0x612A)
#define E_SIO_CLOSE_OBXREGWR  (-0x612B)
#define E_SIO_CLOSE_OBXDCTL   (-0x612C)
#define E_SIO_CLOSE_OBXDEV    (-0x612D)
#define E_SIO_CLOSE_OBXREGCL  (-0x612E)
#define E_SIO_CLOSE_RILDCTRL  (-0x612F)
#define E_SIO_CLOSE_RILDEV    (-0x6130)
#define E_SIO_CLOSE_RILSYNC1  (-0x6131)
#define E_SIO_CLOSE_RILSYNC2  (-0x6132)
#define E_SIO_CLOSE_RILSYNC3  (-0x6133)
#define E_SIO_CLOSE_RILEND    (-0x6134)
#define E_SIO_CLOSE_RILEVT    (-0x6135)
#define E_SIO_CLOSE_RILDLL    (-0x6136)
#define E_SIO_RXCLR_PURGE     (-0x6137)
#define E_SIO_TXCLR_PURGE     (-0x6138)
#define E_SIO_PULSE_SETDTR1   (-0x6139)
#define E_SIO_PULSE_SETDTR2   (-0x613A)
#define E_SIO_STATE_GETSTATE  (-0x613B)
#define E_SIO_STATE_BADFLAG   (-0x613C)
#define E_SIO_LINE_GETLINE    (-0x613D)
#define E_SIO_LINE_SIGTERM    (-0x613E)
#define E_SIO_LINE_NODSR      (-0x613F)
#define E_SIO_LINE_NODCD      (-0x6140)
#define E_SIO_LINE_RINGING    (-0x6141)

/*====*/

#define K_SIO_RXBUFSIZE          (4096)
#define K_SIO_TXBUFSIZE          (4096)
#define K_SIO_RXCACHESIZE         (512)
#define K_SIO_TXCACHESIZE         (256)

#define K_SIO_RESTIME1           (2000)
#define K_SIO_RESTIME2           (2000)
#define K_SIO_RILTIME            (4000)
#define K_SIO_RXPOLLTIME           (10)
#define K_SIO_TXPOLLTIME           (10)
#define K_SIO_RXCHKTIME           (100)
#define K_SIO_TXCHKTIME           (100)
#define K_SIO_RDTIMEOUT           (500)

/*----*/

#define K_SIO_MAXCOMPORTS           (9)
#define K_SIO_MAXOBXPORTS          (10)
#define K_SIO_MAXRILPORTS           (9)
#define K_SIO_PORTNUM_HANDLE     (0xFF)

#define K_SIO_BAUDRATES            (10)
#define K_SIO_BAUDRATE_300          (0)
#define K_SIO_BAUDRATE_600          (1)
#define K_SIO_BAUDRATE_1200         (2)
#define K_SIO_BAUDRATE_2400         (3)
#define K_SIO_BAUDRATE_4800         (4)
#define K_SIO_BAUDRATE_9600         (5)
#define K_SIO_BAUDRATE_19200        (6)
#define K_SIO_BAUDRATE_38400        (7)
#define K_SIO_BAUDRATE_57600        (8)
#define K_SIO_BAUDRATE_115200       (9)

#define K_SIO_FLOWCTRLS             (3)
#define K_SIO_FLOWCTRL_NONE         (0)
#define K_SIO_FLOWCTRL_RTSCTS       (1)
#define K_SIO_FLOWCTRL_XONXOFF      (2)

#define K_SIO_DTRUSES               (3)
#define K_SIO_DTRUSE_NONE           (0)
#define K_SIO_DTRUSE_DIRECT         (1)
#define K_SIO_DTRUSE_REOPEN         (2)

#define K_SIO_ERRORFLAGS            (6)
#define K_SIO_ERRORFLAG_GENERAL  (0x01)
#define K_SIO_ERRORFLAG_BREAK    (0x02)
#define K_SIO_ERRORFLAG_FRAME    (0x04)
#define K_SIO_ERRORFLAG_PARITY   (0x08)
#define K_SIO_ERRORFLAG_HWOVR    (0x10)
#define K_SIO_ERRORFLAG_SWOVR    (0x20)

/*--------------*/
/*  STRUCTURES  */
/*--------------*/

struct sio_config
{
  volatile unsigned char *sigterm;
  unsigned char portnum;
  HANDLE        porthnd;
  unsigned char obxport;
  unsigned char rilport;
  unsigned char baudrate;
  unsigned char flowctrl;
  unsigned char usedtr;
  unsigned char usedsr;
  unsigned char usedcd;
  unsigned char usering;
};

struct sio_data
{
  struct sio_config cfg;
  HANDLE            siohnd;
  HKEY              obxreghnd;
  DWORD             obxregval;
  HANDLE            obxdevhnd;
  HMODULE           rildllhnd;
  HRESULT          (*ril_init)(DWORD index, void *result, void *notify, \
                               DWORD notifyclass, DWORD par, HANDLE *rilhnd);
  HRESULT          (*ril_end) (HANDLE rilhnd);
  HRESULT          (*ril_regs)(HANDLE rilhnd);
  HANDLE            rilusrhnd;
  HANDLE            riljobevt;
  HANDLE            rildevhnd;
  unsigned char     threads;
  CRITICAL_SECTION  winlock;
  CRITICAL_SECTION  errlock;
  unsigned char     rxerror;
  unsigned short    rxcacherd;
  unsigned short    rxcachewr;
  unsigned char     txcacheda;
  unsigned short    txcacherd;
  unsigned short    txcachewr;
  unsigned char     rxcache[K_SIO_RXCACHESIZE];
  unsigned char     txcache[K_SIO_TXCACHESIZE];
};

/*--------------*/
/*  PROTOTYPES  */
/*--------------*/

signed short sio_open    (struct sio_data *dseg, \
                          const struct sio_config *cfg);
signed short sio_close   (struct sio_data *dseg);
signed short sio_threads (struct sio_data *dseg, unsigned char multi);
signed short sio_pulse   (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring);

signed short sio_rxcheck (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring);
signed short sio_txcheck (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring);
signed short sio_rxdelay (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring,     \
                          unsigned short delay);
signed short sio_txdelay (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring,     \
                          unsigned short delay);
signed short sio_rxerror (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring,     \
                          unsigned char clear);
signed short sio_rxlen   (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring);
signed short sio_rxclear (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring);
signed short sio_txclear (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring);
signed short sio_flush   (struct sio_data *dseg, unsigned char term, \
                          unsigned char dcd, unsigned char ring,     \
                          unsigned char slow);

signed short sio_getch (struct sio_data *dseg, unsigned char term, \
                        unsigned char dcd, unsigned char ring,     \
                        unsigned char wait, unsigned char *data);
signed short sio_putch (struct sio_data *dseg, unsigned char term, \
                        unsigned char dcd, unsigned char ring,     \
                        unsigned char slow, unsigned char data);

#ifdef __cplusplus
}
#endif
#endif


/*** EOF ***/
